package com.zx.article.typePay.servlet;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zx.article.typePay.model.TypePayModel;
import com.zx.article.typePay.service.ITypePayService;
import com.zx.article.typePay.service.impl.TypePayServiceImpl;
import com.zx.article.util.FmtRequest;

@WebServlet("/TypePayServlet")
public class TypePayServlet extends HttpServlet {

	private static final long serialVersionUID = 5006927694790361534L;
	private FmtRequest fmtRequest = new FmtRequest();
	private ITypePayService typePayService = new TypePayServiceImpl();

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Object result = "未实现方法";
		String action = request.getParameter("action");
		switch (action) {
		case "get":
			result = get(request);
			break;
		case "getModel":
			result = getModel(request);
			break;
		case "upd":
			result = upd(request);
			break;
		}
		fmtRequest.write(response.getWriter(), result);
	}

	private Object upd(HttpServletRequest request) {
		TypePayModel typePayModel = new TypePayModel();
		typePayModel.setTypeId(request.getParameter("typeId"));
		typePayModel.setArticlePay(new BigDecimal(request.getParameter("articlePay")));
		typePayModel.setCheckPay(new BigDecimal(request.getParameter("checkPay")));
		typePayModel.setPagePay(new BigDecimal(request.getParameter("pagePay")));
		return typePayService.upd(typePayModel);
	}

	private TypePayModel getModel(HttpServletRequest request) {
		return typePayService.getModel(request.getParameter("name"));
	}

	private Map<String, Object> get(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<>();
//		1-count
		TypePayModel typePayModel = new TypePayModel();
		typePayModel.setTypeId(request.getParameter("typename").isEmpty() ? null : request.getParameter("typename"));
		int count = typePayService.getCount(typePayModel);
		map.put("count", count);
//		2-Data
		typePayModel.setPageIndex(Integer.parseInt(request.getParameter("pageIndex")));
		typePayModel.setPageLimit(Integer.parseInt(request.getParameter("pageLimit")));
		typePayModel.setPageOn(true);
		List<TypePayModel> list = typePayService.get(typePayModel);
		map.put("list", list);
		return map;
	}

}
